# This is a sample Python script.
import json
import peewee
import pymysql as pymysql
import requests
import pymysql
import lxml
#coding utf-8
from peewee import MySQLDatabase, Model, CharField, DateField, BooleanField, IntegerField
from bs4 import BeautifulSoup
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

def jsonoperate(str):
    url = str
    head = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'}
    # 头部使用的是chrome的user-Agent
    response = requests.get(url, headers=head, verify=False, timeout=30)
    content = response.text
    for i in range(0, len(content)):
        if(content[i]=='['):
            i=i+1
            if(content[i]=='['):
                x=i
                break
    text = content[x:len(content)-3]
    biglist=[]
    while text.find(']')+4 < len(text):
        text1 = text[text.find('['):text.find(']')]
        text = text[text.find(']') + 4:len(text)]
        smlist = []
        right = 0
        left = 0
        while left <len(text1):
            if(text1[left]=='"'):
                right=left+1
                while(right<len(text1) and text1[right]!='"'):
                    right=right+1
                if(right>=len(text1)):
                    break
                str=text1[left:right+1]
                smlist.append(str)
                left=right
            elif (len(smlist)<4 and text1[left]==','):
                right=left+1
                while(right<len(text1) and text1[right]!=','):
                    right=right+1
                if(right>=len(text1)):
                    break
                str=text1[left+1:right]
                smlist.append(str)
                left=right-1
            left=left+1
        biglist.append(smlist)
    print(text)
    right = 0
    left = 0
    smlist = []
    while left < len(text):
        if text[left] == '"':
            right = left + 1
            while right < len(text) and text[right] != '"':
                right = right + 1
            if right >= len(text):
                break
            str = text[left:right + 1]
            smlist.append(str)
            left = right
        elif len(smlist) < 4 and text[left] == ',':
            right = left + 1
            while right < len(text) and text[right] != ',':
                right = right + 1
            if right >= len(text):
                break
            str = text[left + 1:right]
            smlist.append(str)
            left = right - 1
        left = left + 1
    biglist.append(smlist)
    return biglist


db = peewee.MySQLDatabase(
    host="127.0.0.1",
    user="root",
    password="123456",
    database="study_text",
    port=3306
)
db.connect()

class firstdb (peewee.Model):
    number = peewee.CharField(max_length=30)
    name = peewee.CharField(max_length=30)
    price = peewee.CharField(max_length=30)
    ud = peewee.CharField(max_length=30)
    f = peewee.CharField(max_length=30)
    l = peewee.CharField(max_length=30)
    class Meta:
        database = db


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')
    if firstdb.table_exists():
        firstdb.drop_table()
    firstdb.create_table()
    str = input("请输入你要进行爬虫的网址：")
    big = jsonoperate(str)
    x = 1
    for i in big:
        firstdb.insert(number=i[0],name=i[1],price=i[2],ud=i[3],f=i[4],l=i[5]).execute()

